﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Utility;

namespace HaiFang
{
    public partial class UserSettings : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.txtUser.Text = Security.Decrypt(ConfigurationManager.AppSettings["Usr"].ToString());
                this.txtDomain.Text = Security.Decrypt(ConfigurationManager.AppSettings["Domain"].ToString());
            }
        }

        protected void btnSetValue_Click(object sender, System.EventArgs e)
        {
            if (String.IsNullOrEmpty(this.txtPassword.Text) && String.IsNullOrEmpty(this.txtHide.Text))
            {
                this.lblMsg.Text = "密码不能为空！";
                this.btnNext.Enabled = false;
            }
            else
            {
                bool _imperUsr = false;
                bool _isOld = false;
                ConfigSetting SunlikeConfig = new ConfigSetting(Request.MapPath("") + "/web.config");
                if ((!string.IsNullOrEmpty(txtHide.Text)) && (string.IsNullOrEmpty(txtPassword.Text)))
                {
                    _imperUsr = SunlikeConfig.CheckWindowUser(txtUser.Text, txtHide.Text, txtDomain.Text);
                    _isOld = true;
                }
                else
                {
                    _imperUsr = SunlikeConfig.CheckWindowUser(txtUser.Text, txtPassword.Text, txtDomain.Text);
                    _isOld = false;
                }
                if (_imperUsr)
                {
                    System.Security.Principal.IPrincipal wp = new System.Security.Principal.WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent());
                    if (wp.IsInRole("BUILTIN\\Administrators"))
                    {
                        if (!_isOld)
                        {
                           Session["ImpersonateUsr"] = txtUser.Text;
                           Session["ImpersonateUsrPswd"] = txtPassword.Text;
                           Session["ImpersonateUsrDomain"] = txtDomain.Text;
                        }

                        lblMsg.Text = "模拟成功！";
                        btnNext.Visible = true;
                        btnNext.Enabled = true;
                    }
                    else
                    {
                        lblMsg.Text = "用户必须是管理员，该用户权限不足！";
                        this.btnNext.Enabled = false;
                    }
                }
                else
                {
                    lblMsg.Text = "模拟失败！请检查你的输入是否正确。";
                    this.btnNext.Enabled = false;
                }
            }
        }


        protected void btnNext_Click(object sender, System.EventArgs e)
        {
            Response.Redirect("SqlSettings.aspx");
        }
    }
}